import React,{useState,useEffect} from 'react';
import {Table,Button,Modal,notification,Tag} from 'antd'
import axios from '_axios@0.27.2@axios';
import { FileDoneOutlined,DeleteOutlined,ExclamationCircleOutlined } from '@ant-design/icons';
const AuditList = (props) => {
  const [dataSource, setdataSource] = useState([])
  const columns = [
     {
      title: '新闻标题',
      dataIndex: 'title',
      render: (title,item) => {
        return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
      }
    },
    {
      title: '作者',
      dataIndex: 'author'
    },
    {
      title: '新闻分类',
      dataIndex: 'category',
      render: (category) => {
        return category.title
      }
    },
     {
      title: '审核状态',
      dataIndex: 'auditState',
       render: (auditState) => {
         const tagColor = ["default", "warning", "success", "error"]
         const tagText = ["草稿箱","审核中","已通过","未通过"]
        return <Tag color={tagColor[auditState]}>{tagText[auditState]}</Tag>
      }
    },
    {
      title: '操作',
      render: (item) => {
        return <div>
          {
            item.auditState === 1 &&  <Button
             onClick={() => confirmMethod(item)}
           > 撤回</Button>
          }
          {
            item.auditState === 2 &&  <Button
             danger
             icon={<FileDoneOutlined />}
             onClick={() => handlePublish(item)}
           >发布</Button>
          }
          {
            item.auditState === 3 &&  <Button
             type="primary"
             icon={<DeleteOutlined />}
              onClick={() => {
                 props.history.push(`/news-manage/update/${item.id}`)
              }
            }
           >编辑</Button>
          }
       </div>
      }
    }
  ]
  // 发布
  const handlePublish = (item) => {
     axios.patch(`/news/${item.id}`, {
      publishState: 2
    }).then(res => {
      props.history.push('/publish-manage/published')
       notification.info({
        message: `通知`,
        description:`您可以在【发布管理/已经发布】中查看您的新闻`,
        placement:'bottomRight'
      });
    })
  }
  // 审核
  const handleCheck = (id) => {
    axios.patch(`/news/${id}`, {
      auditState: 1
    }).then(res => {
      props.history.push('/audit-manage/list')
       notification.info({
        message: `通知`,
        description:`您可以在审核列表查看`,
        placement:'bottomRight'
      });
    })
  }
    // 撤回
  const confirmMethod = (item) => {
   setdataSource(dataSource.filter(data => data.id !== item.id))
     axios.patch(`/news/${item.id}`, {
      auditState: 0
    }).then(res => {
       notification.info({
        message: `通知`,
        description:`您可以在草稿箱查看`,
        placement:'bottomRight'
      });
    })
  }
  useEffect(() => {
    const {username} = JSON.parse(localStorage.getItem('token'))
    axios.get(`/news?author=${username}&auditState_ne=0&publishState_lte=1&_expand=category`).then(res => {
      setdataSource(res.data)
    })
  }, [])
  return (
    <div>
      <Table
        dataSource={dataSource}
        columns={columns}
        pagination={{
          pageSize: 5
        }}
        rowKey={(item)=>item.id}
      ></Table>
    </div>
  );
}

export default AuditList;


